//
//  XLComposeView.swift
//  SinaProjectTest
//
//  Created by zhanghaifeng on 16/10/31.
//  Copyright © 2016年 BBSWaitting. All rights reserved.
//

import UIKit

// 罩层
class XLComposeView: UIView {
    
    // MARK: 懒加载
    // 模糊背景图片
    fileprivate lazy var bgImageView: UIImageView = UIImageView(image: self.getScreenShot().applyLightEffect())
    // 标语图片compose_slogan
    fileprivate lazy var sloganImageView: UIImageView = UIImageView(imgNamed: "compose_slogan")
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 设置大小
        size = CGSize(width: XLSCREENW, height: XLSCREENH)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 创建视图
    private func setupUI(){
        // 添加控件
        addSubview(bgImageView)
        addSubview(sloganImageView)
        // 设置约束
        sloganImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(bgImageView)
            make.top.equalTo(bgImageView).offset(110)
        }
        
//        addChildButton(imageNamed: "photo", title: "照相/摄影")
        var buttonM: [XLComposeMenuButton] = [XLComposeMenuButton]()
        let button0 = addChildButtonMethod(imageNamed: "idea", title: "文字")
        let button1 = addChildButtonMethod(imageNamed: "photo", title: "照片/视频")
        let button2 = addChildButtonMethod(imageNamed: "weibo", title: "头条文章")
        let button3 = addChildButtonMethod(imageNamed: "lbs", title: "签到")
        let button4 = addChildButtonMethod(imageNamed: "shooting", title: "直播")
        let button5 = addChildButtonMethod(imageNamed: "more", title: "更多")
        // 添加到数组中
        buttonM.append(button0 as! XLComposeMenuButton)
        buttonM.append(button1 as! XLComposeMenuButton)
        buttonM.append(button2 as! XLComposeMenuButton)
        buttonM.append(button3 as! XLComposeMenuButton)
        buttonM.append(button4 as! XLComposeMenuButton)
        buttonM.append(button5 as! XLComposeMenuButton)
        
        // 按钮的宽度
        let buttonWidth: CGFloat = 80
        // 按钮的高度
        let buttonHeight: CGFloat = 110
        // 计算间距
        let buttonMargin = (width - buttonWidth * 3) * 0.25
        for i in 0..<buttonM.count{
            // 计算列的索引
            let colIndex = i % 3
            // 计算行的索引
            let rowIndex = i / 3
//            let button = XLComposeMenuButton()
            // 设置大小
            buttonM[i].size = CGSize(width: buttonWidth, height: buttonHeight)
            // 设置x y 坐标
            buttonM[i].x = CGFloat(colIndex) * buttonWidth + CGFloat(colIndex + 1) * buttonMargin
            buttonM[i].y = CGFloat(rowIndex) * buttonHeight + CGFloat(rowIndex) * (buttonMargin + 15)
            buttonM[i].tag = i
            // 添加按钮
            addSubview(buttonM[i])
        }

    }
    
    // 按钮点击的 监听方方法
    @objc private func composeMenuBtnClick(button: XLComposeMenuButton){
        switch button.tag {
        case 0:
            print("文字按钮被点击了")
        case 1:
            print("照片按钮被点击了")
        case 2:
            print("头条按钮被点击了")
        case 3:
            print("签到按钮被点击了")
        case 4:
            print("直播按钮被点击了")
        default:
            print("更多按钮被点击了")
        }
    }
    
    
    // 添加按钮的方法
    private func addChildButtonMethod(imageNamed: String, title: String) -> UIButton{
        
        let button = XLComposeMenuButton()
        button.setImage(UIImage(named: "tabbar_compose_\(imageNamed)"), for: .normal)
        button.setTitle(title, for: .normal)
        button.addTarget(self, action: #selector(composeMenuBtnClick), for: .touchUpInside)
        
        return button
    }
    
    // 添加6个自按钮的方法
    private func addChildButton(imageNamed: String, title: String){
        
        // 按钮的宽度
        let buttonWidth: CGFloat = 80
        // 按钮的高度
        let buttonHeight: CGFloat = 110
        // 计算间距
        let buttonMargin = (width - buttonWidth * 3) * 0.25
        
        
        for i in 0..<6{
            
            // 计算列的索引
            let colIndex = i % 3
            // 计算行的索引
            let rowIndex = i / 3
            let button = XLComposeMenuButton()
            
            button.setImage(UIImage(named: "tabbar_compose_\(imageNamed)"), for: .normal)
            button.setTitle(title, for: .normal)
            // 设置大小
            button.size = CGSize(width: buttonWidth, height: buttonHeight)
            // 设置x y 坐标
            button.x = CGFloat(colIndex) * buttonWidth + CGFloat(colIndex + 1) * buttonMargin
            button.y = CGFloat(rowIndex) * buttonHeight + CGFloat(rowIndex) * (buttonMargin + 15)
            // 添加按钮
            addSubview(button)
        }
        
        
    }
    
    // 截取当前屏幕的图片
    func getScreenShot() -> UIImage{
        // 获取当前的 主window
        let window = UIApplication.shared.keyWindow!
        
        // 开启图片上下文
        UIGraphicsBeginImageContext(window.size)
        let rect = CGRect(origin: CGPoint.zero, size: window.size)
        // 根据指定区域绘制
        // afterScreenUpdates 是否等到屏幕更新完再绘制
        window.drawHierarchy(in: rect, afterScreenUpdates: false)
        // 通过图片上下文 获取绘制的图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        // 关闭图片上下文
        UIGraphicsEndImageContext()
        
        return image!
    }
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        removeFromSuperview()
    }
}
